package com.tlcx.kfip.ui;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.tlcx.kfip.R;
import com.tlcx.library.utils.KeyboardUtils;
import com.tlcx.library.utils.TypeConvertUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 充值页面中选择价格view
 * Created by victor on 2016/11/17 17:57.
 * Email:zhuzhaoyang@yuanchuangyun.com
 */

public class SelectAmountView extends LinearLayout{

    @BindView(R.id.rg_first_line)               RadioGroup firstLineRg;           //第一行的radiogroup
    @BindView(R.id.rg_second_line)              RadioGroup secondLineRg;          //第二行的radiogroup
    @BindView(R.id.et_other_amount)             EditText otherAmountEt;           //其他金额

    @BindView(R.id.rb_ten) RadioButton          tenRb;
    @BindView(R.id.rb_fifty) RadioButton        fiftyRb;
    @BindView(R.id.rb_twenty) RadioButton       twentyRb;
    @BindView(R.id.rb_hundred) RadioButton      hundredRb;
    @BindView(R.id.rb_two_hundred) RadioButton  twoHundredRb;


    private Context mContext;
    public SelectAmountView(Context context) {
        super(context);
        initView(context);
        setView();
    }

    public SelectAmountView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        setView();
    }

    public SelectAmountView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        setView();
    }

    /**
     * 初始化
     */
    private void initView(Context context){
        mContext = context;
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = mInflater.inflate(R.layout.widget_select_amount,null,false);
        addView(view);
        ButterKnife.bind(this,view);
    }

    /**
     * 设置
     */
    private void setView(){
        //默认将第一行第一个选中
        RadioButton radioButton1 = (RadioButton) firstLineRg.getChildAt(0);
        radioButton1.setChecked(true);

        //默认输入框设置光标不可见
        otherAmountEt.setCursorVisible(false);
        //使EditText默认不弹出软键盘
        KeyboardUtils.hideSoftInput2((Activity) mContext);

        //设置第一行单选按钮监听
        firstLineRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (radioGroup.getCheckedRadioButtonId()){
                    case R.id.rb_ten:
                        if (tenRb.isChecked()){
                            clearCheck(0);
                        }
                        break;
                    case R.id.rb_twenty:
                        if (twentyRb.isChecked()) {
                            clearCheck(0);
                        }
                        break;
                    case R.id.rb_fifty:
                        if (fiftyRb.isChecked()) {
                            clearCheck(0);
                        }
                        break;
                        default:
                            break;
                }
            }
        });

        //设置第二行单选按钮监听
        secondLineRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (radioGroup.getCheckedRadioButtonId()){
                    case R.id.rb_hundred:
                        if (hundredRb.isChecked()) {
                            clearCheck(1);
                        }
                        break;
                    case R.id.rb_two_hundred:
                        if (twoHundredRb.isChecked()) {
                            clearCheck(1);
                        }
                        break;
                    default:
                        break;
                }
            }
        });

        //设置输入框点击监听
        otherAmountEt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                //设置光标可见
                otherAmountEt.setCursorVisible(true);
                //设置别选中背景
                otherAmountEt.setBackgroundResource(R.drawable.shape_recharge_btn_pressed);
                //清空其他被选中
                firstLineRg.clearCheck();
                secondLineRg.clearCheck();
            }
        });
    }

    /**
     * 清空其他被选中
     * @param flag
     */
    private void clearCheck(int flag){
        if (flag == 0) {
            secondLineRg.clearCheck();
        }else {
            firstLineRg.clearCheck();
        }
        otherAmountEt.setCursorVisible(false);
        otherAmountEt.setBackgroundResource(R.drawable.shape_recharge_btn_normal);
        KeyboardUtils.closeKeyboard(mContext,otherAmountEt);
    }

    /**
     * 获取选中的价格
     */
    public int getSelectedAmount(){
        if (tenRb.isChecked()){
            return 10;
        }
        if (twentyRb.isChecked()){
            return 20;
        }
        if (fiftyRb.isChecked()){
            return 50;
        }
        if (hundredRb.isChecked()){
            return 100;
        }
        if (twoHundredRb.isChecked()){
            return 200;
        }
        String selectedAmount = otherAmountEt.getText().toString();
        if (!TextUtils.isEmpty(selectedAmount)){
           return TypeConvertUtils.String2Int(selectedAmount);
        }
        return 0;
    }
}
